package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.cardbaobao.cardbabyclient.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 字母列表View
 * Created by zad
 * 2016/8/15 20:05
 */

public class LetterListView extends View {
    /**被选中的字体颜色*/
    private int mCheckedColor = 0xFFFFFFFF;
    /**正常的字体颜色*/
    private int mUnCheckColor = 0xFF6C6C6C;
    /**背景色*/
    private int mBgColor =0x50000000;
    /**字体大小 SP单位*/
    private int mTextSize = 14;

    private Paint mPaint;

    private Rect mBound;

    private int mWidth, mHeight, mSelPos = -1;

    private List<String> pinyinList = new ArrayList<>();
    /**
     * 是否显示背景
     */
    private boolean isShowBg;
    /**
     * 字母列表长度
     */
    private int length;

    public LetterListView(Context context) {
        this(context, null);
    }

    public LetterListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LetterListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mTextSize = DensityUtil.sp2px(getContext(), mTextSize);
        mPaint.setTextSize(mTextSize);
        mBound = new Rect();
    }

    public void setPinyinList(List<String> pinyinList) {
        this.pinyinList = pinyinList;
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        length = pinyinList.size();
        if (length == 0) return;

        if (isShowBg) canvas.drawColor(mBgColor); //绘制半透明背景

        mWidth = getWidth();
        mHeight = getHeight();
        //每个字母能分到的高度
        int childAvgHeight = mHeight / length;

        for (int i = 0; i < length; i++) {
//            if (mSelPos == i) {
//                mPaint.setColor(mCheckedColor);
//            } else {
//                mPaint.setColor(mUnCheckColor);
//            }
            mPaint.setColor(0xffffab18);
            String text = pinyinList.get(i);
            if(text.length()>1){
                text=text.substring(0,1);
            }
            mPaint.getTextBounds(text, 0, text.length(), mBound);
            canvas.drawText(text, (mWidth - mBound.width()) / 2, childAvgHeight * i + (childAvgHeight + mBound.height()) / 2, mPaint);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float y = event.getY();
        int lastPost = mSelPos;//上一个选中的位置
        int currentPos = (int) (y / mHeight * length);//当前选中的字母下标
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                isShowBg = true;
                if (lastPost != currentPos && listener != null && currentPos >= 0 && currentPos < length) {
                    listener.onTouchingLetterChanged(pinyinList.get(currentPos), true);
                    mSelPos = currentPos;
                }
                break;
            case MotionEvent.ACTION_UP:
                isShowBg = false;
                mSelPos = -1;
                if(listener != null){
                    listener.onTouchingLetterChanged("",false);
                }
                break;
        }
        invalidate();
        return true;
    }


    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.listener = onTouchingLetterChangedListener;
    }

    private OnTouchingLetterChangedListener listener;

    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String text, boolean isShow);
    }

}
